using System;
using System.Runtime.InteropServices;
namespace AppToolKit.Core.Win32
{
	public class WindowServiceApi
	{
		[Flags]
		public enum SERVICE_TYPES
		{
			SERVICE_KERNEL_DRIVER = 1,
			SERVICE_FILE_SYSTEM_DRIVER = 2,
			SERVICE_WIN32_OWN_PROCESS = 16,
			SERVICE_WIN32_SHARE_PROCESS = 32,
			SERVICE_INTERACTIVE_PROCESS = 256
		}
		public enum SERVICE_STATE : uint
		{
			SERVICE_STOPPED = 1u,
			SERVICE_START_PENDING,
			SERVICE_STOP_PENDING,
			SERVICE_RUNNING,
			SERVICE_CONTINUE_PENDING,
			SERVICE_PAUSE_PENDING,
			SERVICE_PAUSED
		}
		[StructLayout(LayoutKind.Sequential, Pack = 1)]
		public struct SERVICE_STATUS
		{
			public static readonly int SizeOf = Marshal.SizeOf(typeof(WindowServiceApi.SERVICE_STATUS));
			public WindowServiceApi.SERVICE_TYPES dwServiceType;
			public WindowServiceApi.SERVICE_STATE dwCurrentState;
			public uint dwControlsAccepted;
			public uint dwWin32ExitCode;
			public uint dwServiceSpecificExitCode;
			public uint dwCheckPoint;
			public uint dwWaitHint;
		}
		[Flags]
		public enum SERVICE_CONTROL : uint
		{
			STOP = 1u,
			PAUSE = 2u,
			CONTINUE = 3u,
			INTERROGATE = 4u,
			SHUTDOWN = 5u,
			PARAMCHANGE = 6u,
			NETBINDADD = 7u,
			NETBINDREMOVE = 8u,
			NETBINDENABLE = 9u,
			NETBINDDISABLE = 10u,
			DEVICEEVENT = 11u,
			HARDWAREPROFILECHANGE = 12u,
			POWEREVENT = 13u,
			SESSIONCHANGE = 14u
		}
		[Flags]
		public enum SERVICE_ACCEPT : uint
		{
			STOP = 1u,
			PAUSE_CONTINUE = 2u,
			SHUTDOWN = 4u,
			PARAMCHANGE = 8u,
			NETBINDCHANGE = 16u,
			HARDWAREPROFILECHANGE = 32u,
			POWEREVENT = 64u,
			SESSIONCHANGE = 128u
		}
		public const int SC_MANAGER_CREATE_SERVICE = 2;
		public const int SC_MANAGER_CONNECT = 1;
		public const int SERVICE_WIN32_OWN_PROCESS = 16;
		public const int SERVICE_INTERACTIVE_PROCESS = 256;
		public const int SERVICE_ERROR_NORMAL = 1;
		public const int STANDARD_RIGHTS_REQUIRED = 983040;
		public const int SERVICE_QUERY_CONFIG = 1;
		public const int SERVICE_CHANGE_CONFIG = 2;
		public const int SERVICE_QUERY_STATUS = 4;
		public const int SERVICE_ENUMERATE_DEPENDENTS = 8;
		public const int SERVICE_START = 16;
		public const int SERVICE_STOP = 32;
		public const int SERVICE_PAUSE_CONTINUE = 64;
		public const int SERVICE_INTERROGATE = 128;
		public const int SERVICE_USER_DEFINED_CONTROL = 256;
		public const int SERVICE_ALL_ACCESS = 983551;
		public const int SERVICE_AUTO_START = 2;
		[DllImport("advapi32.dll")]
		public static extern IntPtr OpenSCManager(string lpMachineName, string lpSCDB, int scParameter);
		[DllImport("Advapi32.dll")]
		public static extern IntPtr CreateService(IntPtr SC_HANDLE, string lpSvcName, string lpDisplayName, int dwDesiredAccess, int dwServiceType, int dwStartType, int dwErrorControl, string lpPathName, string lpLoadOrderGroup, int lpdwTagId, string lpDependencies, string lpServiceStartName, string lpPassword);
		[DllImport("advapi32.dll")]
		public static extern void CloseServiceHandle(IntPtr SCHANDLE);
		[DllImport("advapi32.dll")]
		public static extern int StartService(IntPtr SVHANDLE, int dwNumServiceArgs, string lpServiceArgVectors);
		[DllImport("advapi32.dll", SetLastError = true)]
		public static extern IntPtr OpenService(IntPtr SCHANDLE, string lpSvcName, int dwNumServiceArgs);
		[DllImport("advapi32.dll")]
		public static extern int DeleteService(IntPtr SVHANDLE);
		[DllImport("kernel32.dll")]
		public static extern int GetLastError();
		[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
		public static extern bool QueryServiceStatus(IntPtr hService, ref WindowServiceApi.SERVICE_STATUS dwServiceStatus);
		[DllImport("advapi32.dll", SetLastError = true)]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool ControlService(IntPtr hService, WindowServiceApi.SERVICE_CONTROL dwControl, ref WindowServiceApi.SERVICE_STATUS lpServiceStatus);
	}
}
